/*
 * PatientLogin.java
 * 
 *  author: Daniel Rudigier
 *    date: 24.06.2008
 * 
 * 
 */
package ocumed.applikation.benutzerverwaltung;

import java.util.Iterator;
import java.util.List;

import ocumed.applikation.exceptions.LoginFailedException;
import ocumed.persistenz.dao.DAOFactory;
import ocumed.persistenz.dao.PatientloginDAO;
import ocumed.persistenz.hibernate.OcPatientlogin;
import ocumed.teams.IArzt;
import ocumed.teams.IPatient;
import ocumed.teams.OcumedFactory;

/**
 * provides login use-case functionality for a patient
 * 
 * @author Daniel Rudigier
 * 
 */
public class PatientLogin {
	private IPatient m_patient;
	private PatientloginDAO plDAO;

	/**
	 * constructor
	 * 
	 * @throws LoginFailedException
	 * 
	 */
	public PatientLogin(String username, String password)
			throws LoginFailedException {
	    
		plDAO = DAOFactory.get(DAOFactory.RDBMS).getPatientloginDAO();
		plDAO.start();
		List<OcPatientlogin> logins = plDAO.findAll();
		Iterator<OcPatientlogin> iter = logins.iterator();
		OcPatientlogin currentlogin = null;

		while (iter.hasNext()) {
			currentlogin = iter.next();
			if (currentlogin.getPlusername().equals(username)
					&& currentlogin.getPlpasswort().equals(password)) {
				m_patient = OcumedFactory.getInstance().getiPatient(
						currentlogin.getPatientid());
				break;
			}
		}

		if (m_patient == null) {
			throw new LoginFailedException();
		}
	}

	public void logout() {
		m_patient = null;
	}

	/**
	 * 
	 * @return
	 */
	public Integer getArztId() {
		return m_patient.getiBenutzerByArztid().getBenutzerid();
	}

	/**
	 * 
	 * @return
	 */
	public Integer getPatientId() {
		return m_patient.getPatientid();
	}

	/**
	 * 
	 * @return
	 */
	public String getPatientName() {
		return m_patient.getPatientnachname() + " "
				+ m_patient.getPatientvorname();
	}

	/**
	 * 
	 * @return
	 */
	public String getArztName() {
		IArzt a = m_patient.getiBenutzerByArztid();
		return a.getBenutzernachname() + " " + a.getBenutzervorname();
	}

}
